package com.example.ayareimburse;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Collections;

import android.os.Environment;

public class FileProcessor 
{
	private final String ROOT = Environment.getExternalStorageDirectory().getAbsolutePath() + "/data/com.zynga/";
	private final String SAVEROOT = Environment.getExternalStorageDirectory().getAbsolutePath() + "/data/saves/";
	private final String FILENAME = "zynga.properties";
	
	private ArrayList<String> srlFileNames = new ArrayList<String>();
	
	/*
	 * Function: Get current using save file number
	 */
	public String getCurrentFile()
	{
		String strRtn = "";	
 		
		// Get all saves from save folder
 		File fileSaves = new File(SAVEROOT);
 		File[] files = fileSaves.listFiles();
 		srlFileNames = new ArrayList<String>();
 		
 		// Save all save file names into an array
 		for (int i = 0; i < files.length; i++)
 		{
 			srlFileNames.add(files[i].getName());
 		}
 		Collections.sort(srlFileNames);
 		
 		if (srlFileNames.get(0).substring(0,1).equals("-"))
 		{
 			// Using old saves, return save file number
 			strRtn = srlFileNames.get(0).substring(1,4);
 		} 
 		else
 		{
 			// Creating new save, return "-"&&new file number
 			strRtn = "-" + srlFileNames.get(srlFileNames.size() - 1).substring(0,3);
 		}
 		
		return strRtn;
	}
	
	/*
	 * Function: Create new save file
	 */
	public void createNew()
	{
		
		if (srlFileNames.get(0).substring(0,1).equals("-"))
	 	{
			//"-00X" existed, delete current save file
			File fileCntSave = new File(ROOT + FILENAME);
			if (fileCntSave.isDirectory())
			{
				fileCntSave.delete();
			}
	 	}
		else
		{
			//"-00X" does not existed, move current save to SAVEROOT
			int intCntNum = Integer.parseInt(srlFileNames.get(srlFileNames.size() - 1).substring(0,3));
			String strNewName = String.format("%03d", (intCntNum +1)) + FILENAME;
			moveFile(ROOT + FILENAME, SAVEROOT, strNewName);			
		}
		
	}
	
	public void changeSave()
	{
		if (srlFileNames.get(0).substring(0,1).equals("-"))
	 	{
			//"-00X" existed, delete current save file
			File fileCntSave = new File(ROOT + FILENAME);
			if (fileCntSave.isDirectory())
			{
				fileCntSave.delete();
			}
			
			// get next save
			String strCntSave = srlFileNames.get(0).substring(1,4);
			String strNextSave = String.format("%03d", Integer.parseInt(strCntSave) + 1) + FILENAME;
			
			//Check next save exist or not 
			if (srlFileNames.contains(strNextSave))
			{
				copyFile(SAVEROOT + strNextSave, ROOT + FILENAME);
				moveFile(SAVEROOT + srlFileNames.get(0), SAVEROOT, srlFileNames.get(0).substring(1));
				moveFile(SAVEROOT + strNextSave, SAVEROOT, "-" + strNextSave);
			}
			else
			{
				// back to the beginning
				copyFile(SAVEROOT + "001" + FILENAME, ROOT + FILENAME);
				moveFile(SAVEROOT + srlFileNames.get(0), SAVEROOT, srlFileNames.get(0).substring(1));
				moveFile(SAVEROOT + "001" + FILENAME, SAVEROOT, "-" + "001" + FILENAME);
			}
	 	}
		else
		{
			//"-00X" does not existed, move current save to SAVEROOT
			int intCntNum = Integer.parseInt(srlFileNames.get(srlFileNames.size() - 1).substring(0,3));
			String strNewName = String.format("%03d", (intCntNum +1)) + FILENAME;
			moveFile(ROOT + FILENAME, SAVEROOT, strNewName);
			
			// back to the beginning
			copyFile(SAVEROOT + "001" + FILENAME, ROOT + FILENAME);
			moveFile(SAVEROOT + "001" + FILENAME, SAVEROOT, "-" + "001" + FILENAME);
		}
		
	}
	
	public void getMainSave()
	{
		// Delete current 
		File fileCntSave = new File(ROOT + FILENAME);
		if (fileCntSave.isDirectory())
		{
			fileCntSave.delete();
		}
		
		// Copy main save
		copyFile(SAVEROOT + "000" + FILENAME, ROOT + FILENAME);
	}
	
	public void getLastSaveBack()
	{
		// Delete current 
		File fileCntSave = new File(ROOT + FILENAME);
		if (fileCntSave.isDirectory())
		{
			fileCntSave.delete();
		}
		
		// Copy last save back
		String strCntSave = srlFileNames.get(0);
		copyFile(SAVEROOT + strCntSave, ROOT + FILENAME);
	}
	
	private void moveFile(String pOldPath, String pNewDir, String pNewName) 
	{   
        File file = new File(pOldPath);
        File dir = new File(pNewDir); 
        file.renameTo(new File(dir, pNewName));  
	}  
	
	@SuppressWarnings("resource")
	private void copyFile(String pOldPath, String pNewPath)
	{
		File iFile = new File(pOldPath);
		File oFile = new File(pNewPath);
		try {
			FileChannel iChannel = new FileInputStream(iFile).getChannel();
			FileChannel oChannel = new FileOutputStream(oFile).getChannel();
			iChannel.transferTo(0, iChannel.size(), oChannel);
			iChannel.close();
			oChannel.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		} 
	}
	
	
}
